package com.mtz.controller;

import com.google.gson.Gson;
import com.mtz.VO.QuesAnsVO;
import com.mtz.VO.ResultVO;
import com.mtz.dateobject.UserServer;
import com.mtz.service.ExcelService;
import com.mtz.service.QuesAnsService;
import com.mtz.utils.JsonUtil;
import com.mtz.utils.ResultVOUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(description = "Excel接口")
@RestController
@RequestMapping(value = "/excel")
@Slf4j
public class ExcelController {

    @Autowired
    private QuesAnsService quesAnsService;

    @Autowired
    private ExcelService excelService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    //生成user表excel
    @ApiOperation(value = "导出所有问答", notes = "导出所有问答")
    @GetMapping("/download")
    public String getUser(HttpServletResponse response) throws Exception{
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("统计表");
        excelService.createTitle(workbook,sheet);

        List<QuesAnsVO> rows = quesAnsService.findAll();

        //设置日期格式
        HSSFCellStyle style = workbook.createCellStyle();
        style.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));

        //新增数据行，并且设置单元格数据
        int rowNum = 1;
        for(QuesAnsVO quesAnsVO : rows){

            HSSFRow row = sheet.createRow(rowNum);
            row.createCell(0).setCellValue(quesAnsVO.getId());
            row.createCell(1).setCellValue(quesAnsVO.getKnowledgeType());
            row.createCell(2).setCellValue(quesAnsVO.getQuestion());
            row.createCell(3).setCellValue(quesAnsVO.getKeyword());
            row.createCell(4).setCellValue(quesAnsVO.getAnswer());
            row.createCell(5).setCellValue(quesAnsVO.getAuthor());
            row.createCell(6).setCellValue(quesAnsVO.getAngle());
            rowNum++;
        }

        String fileName = "iSchoolMtz问答知识库.xls";

        //生成excel文件
        excelService.buildExcelFile(fileName, workbook);

        //浏览器下载excel
        excelService.buildExcelDocument(fileName,workbook,response);

        return "download excel";
    }



    //导入excel
    @ApiOperation(value = "导入问答数据", notes = "导入问答数据")
    @PostMapping("/upload")
    public ResultVO importInfo(ServletRequest servletRequest,
                               @ApiParam(value = "选择要上传的excel文件",required = true)
                                   @RequestParam("file") MultipartFile file) {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String token = request.getHeader("token");
        String tokenValue = redisTemplate.opsForValue().get(token);

        Gson gson = new Gson();
        UserServer userServer = gson.fromJson(tokenValue, UserServer.class);
        String author = userServer.getUsername();
        log.info("userServer:{}", JsonUtil.toJson(userServer));

        boolean a = false;
        String message;
        String fileName = file.getOriginalFilename();
        try {
            a = excelService.batchImport(fileName, file, author);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (a) {
            message = "导入成功!";
        } else {
            message = "导入失败!";
        }

        return ResultVOUtil.success(message);
    }


}